<?php
// ini_set('session.gc_maxlifetime', 7200);
include("../tools/session.php");
include("../tools/pub.php");
  
$username = trim($_POST["username"]);
$password = trim($_POST["pwd"]);
// $username = 'jzoa';
// $password = '123456';

$no_str = array("未分配公司", "未分配部门", "未分配办公室", "未分配职位", "未分配组");

if ($is_login_success != true)
{
   include("../tools/mysql.php");
   db_connect();
   
   $sql = "SELECT count(id) num FROM user WHERE username='" . $username . "' AND password=MD5('" . $password . "') AND deleted=0";
   $re = db_query_once($sql);

   if ($re["num"] != 0)
	 {
		$sql2 = "SELECT * FROM view_user WHERE username='" . $username . "'";
		/*
		$sql2_backup = "SELECT a.id, a.username, a.job_num, a.power_add, a.power_trim, a.register_time, a.theme,
						b.position_id, b.zh_name position_name, b.`power` position_power,
						c.section_id, c.zh_name section_name, c.master_id, c.`power` section_power,
						d.company_id, d.zh_name company_name, d.`power` company_power,
						e.office_id, e.zh_name office_name, e.`power` office_power,
						f.group_id, f.zh_name group_name, f.`power` group_power
				FROM user a LEFT JOIN position b ON a.position_id = b.position_id
							LEFT JOIN section c ON b.section_id = c.section_id
							LEFT JOIN company d ON c.company_id = d.company_id
							LEFT JOIN office e ON a.office_id = e.office_id
							LEFT JOIN usergroup f ON a.group_id = f.group_id
				WHERE a.deleted=0 AND a.username='" . $username . "'";
		*/
		
		
		$re2 = db_query_once($sql2);
		
		$_SESSION["user"] = array();
		$_SESSION["user"]["id"]				= $re2["id"];
		$_SESSION["user"]["username"]			= $re2["username"];
		$_SESSION["user"]["job_num"]			= $re2["job_num"];
		$_SESSION["user"]["company_id"]		= $re2["company_id"];
		$_SESSION["user"]["company_name"]	= (!$re2["company_name"]) ? $no_str[0] : $re2["company_name"];
		$_SESSION["user"]["section_id"]			= $re2["section_id"];
		$_SESSION["user"]["section_name"]		= (!$re2["section_name"]) ? $no_str[1] : $re2["section_name"];
		$_SESSION["user"]["office_id"]			= $re2["office_id"];
		$_SESSION["user"]["office_name"]		= (!$re2["office_name"]) ? $no_str[2] : $re2["office_name"];
		$_SESSION["user"]["position_id"]			= $re2["position_id"];
		$_SESSION["user"]["position_name"]		= (!$re2["position_name"]) ? $no_str[3] : $re2["position_name"];
		$_SESSION["user"]["group_id"]			= $re2["group_id"];
		$_SESSION["user"]["group_name"]		= (!$re2["group_name"]) ? $no_str[4] : $re2["group_name"];
		$_SESSION["user"]["power_add"]		= $re2["power_add"];
		$_SESSION["user"]["power_trim"]		= $re2["power_trim"];
		$_SESSION["user"]["register_time"]		= $re2["register_time"];
		$_SESSION["user"]["theme"]			= $re2["theme"];
		$_SESSION["user"]["master_id"]			= $re2["master_id"];
		$_SESSION["user"]["is_master"]			= ($re2["master_id"] == $re2["id"]) ? 1 : 0;
		
		/*
		$power_array = array();
		$power_array = array_group_and_flat(array_merge(explode(",", $re2["power_add"]),
								   explode(",", $re2["office_power"]),
								   explode(",", $re2["position_power"]),
								   explode(",", $re2["group_power"]),
								   explode(",", $re2["section_power"]),
								   explode(",", $re2["company_power"]),
								   array("0")));
		$power_array = array_del_from_another($power_array, explode(",", $re2["power_trim"]));
		*/
		$power_array = array(0);
		$_SESSION["user"]["power"] = $power_array;
		
		/*
		$sql3 = "SELECT * FROM view_user WHERE id=" . $re2["master_id"];
		$re3 = db_query_once($sql3);
		$_SESSION["master"] = array();
		$_SESSION["master"]["id"]				= $re3["id"];
		$_SESSION["master"]["username"]		= $re3["username"];
		$_SESSION["master"]["job_num"]		= $re3["job_num"];
		$_SESSION["master"]["company_id"]		= $re3["company_id"];
		$_SESSION["master"]["company_name"]	= (!$re3["company_name"]) ? $no_str[0] : $re3["company_name"];
		$_SESSION["master"]["section_id"]		= $re3["section_id"];
		$_SESSION["master"]["section_name"]	= (!$re3["section_name"]) ? $no_str[1] : $re3["section_name"];
		$_SESSION["master"]["office_id"]		= $re3["office_id"];
		$_SESSION["master"]["office_name"]	= (!$re3["office_name"]) ? $no_str[2] : $re3["office_name"];
		$_SESSION["master"]["position_id"]		= $re3["position_id"];
		$_SESSION["master"]["position_name"]	= (!$re3["position_name"]) ? $no_str[3] : $re3["position_name"];
		$_SESSION["master"]["group_id"]		= $re3["group_id"];
		$_SESSION["master"]["group_name"]		= (!$re3["group_name"]) ? $no_str[4] : $re3["group_name"];
		$_SESSION["master"]["is_master"]		= $re3["is_master"];
		$_SESSION["master"]["power_add"]		= $re3["power_add"];
		$_SESSION["master"]["power_trim"]		= $re3["power_trim"];
		$_SESSION["master"]["register_time"] 	= $re3["register_time"];
		$_SESSION["master"]["theme"]			= $re3["theme"];
		*/
		
		$_SESSION["logined"] = true;
		echo "Success: 登录成功！";
	 }
   else
	 {
		echo "您的姓名或密码错误，请重新登录！";
	 }
}

?>
